<?php
	class RotationAction extends BaseAction{
		private $fileName;
		public function index(){
			$rotation = M('Rotation');
			import("ORG.Util.Page");
			$count = $rotation->count();
			$page = new Page($count,c('PAGESIZE'));
			$show = $page->show();
			$data = $rotation->limit($page->firstRow.','.$page->listRows)
							->field('id,sort,title,title ftitle,link,link flink,state')
							->select();
			getSubStr($data, 'title', 20, 'utf-8');
			getSubStr($data, 'link', 20, 'utf-8');
			$this->assign("list",$data);
			$this->assign('page',$show);
			$this->display();
		}
		
		
		//实现对首页轮播器图片播放顺序的排序
		public function sort(){
			$rotation = M('Rotation');
			$data = array();
			$_effectRows = 0;//记录修改影响行数
			for($i = 0; $i < count($_POST['ids']); $i++)
			{
				if(!is_numeric($_POST['sort'][$i])){
					$this->error('请确保输入的是数字！');
					exit();
				}
				$data['sort'] = (int)$_POST['sort'][$i];
				$result = $rotation->where('id = '.$_POST['ids'][$i])->save($data);
				$_effectRows += $result == false ? 0 : $result;
			}
			if(0 == $_effectRows){
				$this->error('修改轮播器排序失败或没有需要修改的轮播器排序！');
			}else{
				$this->success('修改轮播器排序成功！');
			}

		}
		
		//通过审核
		public function pass(){
			if(empty($_GET['id'])){
				$this->error('请选择要审核的首页轮播器！');
				exit();
			}
			$rotation = M('rotation');
			$rotationData = $rotation->getById($_GET['id']);
			if(!$rotationData){
				$this->error('请确保要审核的首页轮播器ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 1;
			if($rotation->where('id = '.$_GET['id'])->save($data)){
				$this->success('审核成功！');
			}else{
				$this->error('审核失败！');
			}
		
		}
		
		//取消审核
		public function cancel(){
			if(empty($_GET['id'])){
				$this->error('请选择要取消审核的首页轮播器！');
				exit();
			}
			$rotation = M('rotation');
			$rotationData = $rotation->getById($_GET['id']);
			if(!$rotationData){
				$this->error('请确保要取消审核的首页轮播器ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 0;
			if($rotation->where('id = '.$_GET['id'])->save($data)){
				$this->success('取消审核成功！');
			}else{
				$this->error('取消审核失败！');
			}
		}
		
		public function add(){
			$this->display();
		}
		
		public function insert(){
			$rotation = D('rotation');
			if($rotation->create())
			{
				if($rotation->add())
				{
					$this->assign("jumpUrl","__URL__");
					$this->success("添加成功！");
				}else{
					$this->error("添加失败！");
				}
			}else{
				$this->error($rotation->getError());
			}
		}
		
		public function edit()
		{
			if(!empty($_GET['id']))
			{
				$rotation = M('rotation');
				$data = $rotation->getById($_GET['id']);
				// 				var_dump($data);
				if(null == $data)
					$this->error("请确认要修改的首页轮播器ID合法！");
				$this->assign('rotation',$data);
				$this->display();
		
			}else{
				$this->error("请选择要修改的首页轮播器！");
			}
		}
		
		public function update()
		{
			$rotation = D('rotation');
			if($rotation->create()){
				if($rotation->save()){
					$this->success("更新成功！");
				}else{
					$this->error("因为数据没变化或更新失败！");
				}
			}else{
				$this->error($rotation->getError());
			}
		
		}
		
		public function delete()
		{
			if(!empty($_GET['id']))
			{
				$rotation = M('rotation');
				if($rotation->delete($_GET['id'])){
					$this->success("删除首页轮播器成功!");
				}else{
					$this->error("删除首页轮播器失败!");
				}
			}else{
				$this->error("请选择要删除的首页轮播器！");
			}
		}
		
		//更新首页轮播器xml
		public function updateXML(){
			$rotation = M('Rotation');
			$data = $rotation->field('id,thumbnail,link')
							->where('state = 1')
							->order('sort desc,date desc')
							->limit(C('ROTATION_NUM'))
							->select();
			$xml .= '<?xml version="1.0" encoding="utf-8"?>'."\r\n";
			$xml .= '<bcaster autoPlayTime="'.C('ROTATION_TIME').'">'."\r\n";
			if ($data) {
				foreach ($data as $value) {
					$xml .= '<item item_url="'.C('WEB_DIR').'/Public/Uploads/m_'.$value['thumbnail'].'"  link="'.C('WEB_DIR').'/index.php/List/details'.$value['link'].'"  itemtitle=""></item>'."\r\n";
				}
			}
			$xml .= '</bcaster>'."\r\n";
			$_sxe= new SimpleXMLElement($xml);
			if($_sxe->asXML('./bcastr.xml')){
				$this->success('更新首页轮播器成功！');
			}else{
				$this->error('更新首页轮播器失败！');
			}
			
		}
		

		//通过js调用这个页面，用于选择文件上传
		public function showUploadFile(){
			$this->display();
		}

		//接受上传操作，并将上传结果返回，取得上传路径并显示上传图片
		public function upload(){
			// 			dump($_FILES);
			// 			exit();
			$this->fileName = md5_file($_FILES['pic']['tmp_name']);
			if(!empty($_FILES)){
				$result = $this->_upload();
				if(isset($result)){
					$path = C('WEB_DIR').'/Public/Uploads/m_'.$result[0]['savename'];
					$name = $result[0]['savename'];
					echo "<script type='text/javascript'>opener.document.content.thumbnail.value='$name';</script>";
					echo "<script type='text/javascript'>opener.document.content.pic.style.display='block';</script>";
					echo "<script type='text/javascript'>opener.document.content.pic.src='$path';</script>";
					$this->success('上传文件成功！');
					echo "<script type='text/javascript'>window.close();</script>";
					exit();

				}else{
					$this->error('上传文件失败！');
				}
			}else{
				$this->error('请选择要上传的文件！');
			}
		}

		//处理上传过程
		private function _upload(){
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();
			$upload->maxSize = 204800;//上传文件最大大小
			$upload->savePath = './Public/Uploads/';//上传文件保存路径
			$upload->saveRule = $this->fileName;//设置文件名保存规则
			$upload->allowExts=array('jpg','jpeg','png','gif');//设置允许的文件后缀
			$upload->allowTypes=array('image/png','image/jpg','image/pjpeg','image/gif','image/jpeg','image/x-png','image/x-jpg','image/x-pjpeg','image/x-gif','image/x-jpeg');  //检测mime类型
			$upload->thumb=true;   //是否开启图片文件缩略
			$upload->thumbMaxWidth='268';  //以字串格式来传，如果你希望有多个，那就在此处，用,分格，写上多个最大宽
			$upload->thumbMaxHeight='193';	//最大高度
			$upload->thumbPrefix='m_';//缩略图文件前缀
			$upload->uploadReplace=true;
			if($upload->upload()){
				return $upload->getUploadFileInfo();
			}else{
				//echo $upload->getErrorMsg();
				$this->error($upload->getErrorMsg());
			}
		}
	}
?>